package com.eqdd.yiqidian.ui.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseQuickAdapter;
import com.eqdd.yiqidian.adapter.recycleadapter.BaseViewHolder;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.EditBean;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.eqdd.yiqidian.widget.ChooseAlertView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lvzhihao on 17-3-24.
 */

public abstract class BaseEditActivity extends BaseActivity {

    public List<EditBean> editBeans = new ArrayList<>();
    public BaseQuickAdapter<EditBean, BaseViewHolder> quickAdapter;
    private RecyclerView recyclerView;
    private Map<Integer, ActionSheetDialog> bottomChooses = new HashMap<>();
    private Map<Integer, TimePickerView> pvDayTimes = new HashMap<>();
    private Map<Integer, TextView> editContents = new HashMap<>();
    private int clicked;
    private boolean isTip=true;
    private AlertDialog alertDialog;

    @Override
    public void setView() {
        recyclerView = initRecyclerView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setFocusable(false);
        if (quickAdapter == null) {
            quickAdapter = new BaseQuickAdapter<EditBean, BaseViewHolder>(R.layout.item_edit, editBeans) {
                @Override
                protected void convert(BaseViewHolder helper, EditBean item) {
                    TextView content = helper.getView(R.id.tv_vertical_content);
                    if (TextUtils.isEmpty(item.getContent())) {
                        content.setHint(item.getHint());
                        content.setText("");
                    } else {
                        content.setText(item.getContent());
                    }
                    helper.setText(R.id.tv_vertical_title, item.getTitle())
                            .setVisible(R.id.iv_enter, item.isEnter())
                            .setVisible(R.id.iv_bottom, item.isBottom())
                            .setVisible(R.id.iv_top, item.isTop())
                            .setVisible(R.id.iv_line, item.isUnderLine());
                    if (item.isShowTopTitle()) {
                        helper.setVisible(R.id.rl_top, true)
                                .setVisible(R.id.tv_top_title, true)
                                .setText(R.id.tv_top_title, item.getTopTitle());
                    }
                    switch (item.getType()) {
                        case 0:
                        case 4:
                            editContents.put(helper.getPosition(), helper.getView(R.id.tv_vertical_content));
                            break;
                        case 1:
                            bottomChooses.put(helper.getPosition(),
                                    getBottomChoose(item.getChooseItem(), helper.getView(R.id.tv_vertical_content)));
                            break;
                        case 2:
                            pvDayTimes.put(helper.getPosition(),
                                    getTimePicker(Config.YEAR_MONTH_DAY, helper.getView(R.id.tv_vertical_content)));
                            break;
                        case 3:
                            pvDayTimes.put(helper.getPosition(),
                                    getTimePicker(Config.ALL, helper.getView(R.id.tv_vertical_content)));
                            break;

                    }
                }
            };
            quickAdapter.bindToRecyclerView(recyclerView);
            recyclerView.setAdapter(quickAdapter);
            ItemClickSupport.addTo(recyclerView).setOnItemClickListener(
                    (rv, p, v) -> {
                        clicked = p;
                        switch (quickAdapter.getItem(p).getType()) {
                            case 0:
                                Bundle bundle = new Bundle();
                                bundle.putString("content", editContents.get(p).getText().toString());
                                bundle.putString("tip", quickAdapter.getItem(p).getTextTip());
                                bundle.putString("title", quickAdapter.getItem(p).getTextTitle());
                                AppConstent.schemes=quickAdapter.getItem(p).getSchemes();
                                startActivityForResult(EditTextActivity.class, bundle, p);
                                break;
                            case 1://选项选择器
                                bottomChooses.get(p).show();
                                break;
                            case 2://日期选择期 日
                            case 3://日期选择期 日
                                pvDayTimes.get(p).show();
                                break;

                        }
                        itemClick(p);
                    }
            );

        }
    }

    public void refresh() {
        quickAdapter.notifyDataSetChanged();
    }


    private TimePickerView getTimePicker(int type,TextView textView) {
        TimePickerView pvDayTime = new TimePickerView.Builder(this, (date, v) -> {
            SimpleDateFormat sf = null;
            switch (type) {
                case 3:
                    sf = new SimpleDateFormat("yyyy-MM-dd");
                    break;
                case 6:
                    sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    break;
            }
            textView.setText(sf.format(date));
            editBeans.get(clicked).setContent(sf.format(date));
        })
                .setOutSideCancelable(true)
                .setType(type==Config.YEAR_MONTH_DAY?Config.YEAR_MONTH_DAY_TYPE:Config.ALL_TYPE)
                .setDate(Calendar.getInstance())
                .setRange(1950, 2050)
                .build();
        return pvDayTime;
    }

    private ActionSheetDialog getBottomChoose(String[] chooseTexts, TextView textView) {
        ActionSheetDialog bottomChoose = ChooseAlertView.getBottomChoose(this, "请选择", chooseTexts,
                (ob, position) -> {
                    if (position >= 1 && position <= chooseTexts.length) {
                        textView.setText(chooseTexts[position - 1]);
                        editBeans.get(clicked).setContent(chooseTexts[position - 1]);

                    }
                }
        );
        return bottomChoose;
    }


    protected abstract RecyclerView initRecyclerView();

    public abstract void itemClick(int p);

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case Config.INTENT_CONTENT:
                System.out.println(data.getStringExtra("content"));
                editContents.get(requestCode).setText(data.getStringExtra("content"));
                editBeans.get(clicked).setContent(data.getStringExtra("content"));
//                for (int i=0;i<quickAdapter.getItemCount();i++){
//                    editBeans.get(i).setContent(((TextView)quickAdapter.getViewByPosition(i,R.id.tv_vertical_content)).getText().toString());
//                }
//                refresh();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (isTip) {
            isExitEdit();
        } else {
            super.onBackPressed();
        }
    }

    private void isExitEdit() {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("你要退出此次编辑吗?");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", (dialog, which) -> {
                dialog.dismiss();
                finish();

            });
            builder.setNegativeButton("取消", (DialogInterface dialog, int which) -> {
                        dialog.dismiss();
                    }
            );
            alertDialog = builder.create();
        }
        alertDialog.show();
    }


    @Override
    public void onClick(View v) {
        if (isTip) {
            switch (v.getId()) {
                case R.id.back:
                    isExitEdit();
                    break;
                default:
                    super.onClick(v);
                    break;

            }
        } else {
            super.onClick(v);
        }
    }
}
